package com.laijiale.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laijiale.mapper.PermissionMapper;
import com.laijiale.mapper.RoleMapper;
import com.laijiale.Service.RoleService;
import com.laijiale.dto.*;
import com.laijiale.entities.*;
import com.laijiale.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 12:02
 */
@Service
@CacheConfig(cacheNames = "role")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(RoleInsertDTO roleInsertDTO) {
        Role role = BeanUtil.copyProperties(roleInsertDTO, Role.class);
        role.setId(null);
        role.setDeleted(0);
        role.setCreated(LocalDateTime.now());
        role.setUpdated(LocalDateTime.now());
        int result = roleMapper.insert(role);
        if (result <= 0) {
            throw new RuntimeException("DB添加失败");

        }

        return result;
    }

    @Cacheable(key = "#id", condition = "#id !=null", unless = " #result==null")
    @Override
    public Role selectById(Long id) {
        Role result = roleMapper.selectById(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException("ID不存在");
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public int updateById(RoleUpdateDTO roleUpdateDTO) {
        Role role = BeanUtil.copyProperties(roleUpdateDTO, Role.class);
        role.setCreated(null);
        role.setUpdated(LocalDateTime.now());
        int result = roleMapper.updated(role);
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        //todo 查询记录是否被引用
        int result = roleMapper.logicDeleteById(id);
        if (result <= 0) {
            throw new ServiceException("DB逻辑删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        //todo 查询记录是否被引用
        int result = roleMapper.logicDeleteByIds(ids);
        if (result <= 0) {
            throw new ServiceException("DB逻辑批删失败");

        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result==null")
    @Override
    public List<Role> list() {
        return roleMapper.list();
    }

    @Cacheable(key = "#root.methodName + ':' + " +
            "#pageDTO.getPageNum()+':' + " +
            "#pageDTO.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<Role> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());

        List<Role> list = roleMapper.list();
        PageInfo<Role> pageInfo = new PageInfo(list);

        return pageInfo;
    }
@Cacheable(key = "#root.methodName + ':' + #id",condition = "#id != null ",unless = "#result == null ")
    @Override
    public List<Permission> getPermissions(Long id) {
        return permissionMapper.selectByRoleId(id);
    }
    @CacheEvict(allEntries = true)
    @Override
    public int updatePermissions(RoleUpdatePermissionsDTO dto) {
            Long id=dto.getId();
            List<Long> permissionIds=dto.getPermissionIds();
            //删除该员工全部中间表记录
                permissionMapper.logicDeleteByRoleId(id);
                for(Long permissionId:permissionIds){
                RolePermission rolePermission =new RolePermission();
                    rolePermission.setFkPermissionId(id);
                    rolePermission.setFkRoleId(id);
                    rolePermission.setDeleted(0);
                    rolePermission.setCreated(LocalDateTime.now());
                    rolePermission.setUpdated(LocalDateTime.now());
                if (permissionMapper.insertRolePermission(rolePermission)<=0) {
                    throw new ServiceException("添加权限失败");


                }

            }
            return 1;
        }
    @Cacheable(key = "#root.methodName + ':' + " +
            "#dto.getTitle() + ':' + " +
            "#dto.getPageNum() + ':' +" +
            "#dto.getPageSize()",unless = "#result==null")
    @Override
    public PageInfo<Role> pageLikeTitle(RolePageDTO dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        return new PageInfo<>(roleMapper.listLikeTitle(dto.getTitle()));
    }
    }


